package top.wangjiu.java.jvm.attribute;

import top.wangjiu.java.jvm.AttributeInfo;
import top.wangjiu.java.jvm.constantinfo.ConstantPoolInfo;

/**
 * 通知虚拟机自动为静态变量赋值，只有被static关键字修饰的变量（类变量）才可以使用这项属性
 */
public class ConstantValueAttr extends AttributeInfo {

    public final static String NAME = "ConstantValue";

    private final short attributeNameIndex;   //属性名索引
    private final int attributeLength;
    //字段值在常量池中的索引，常量池在该索引处的项给出该属性表示的常量值。（例如，值是long型的，在常量池中便是CONSTANT_Long）
    private final short constantValueIndex;

    public ConstantValueAttr(short attributeNameIndex, byte[] bytes){
        this.attributeNameIndex = attributeNameIndex;
        this.attributeLength = bytes.length;
        this.constantValueIndex = (short) (bytes[0]<<8 | bytes[1]);
    }

    @Override
    public short attributeNameIndex() {
        return attributeNameIndex;
    }

    @Override
    public int attributeLength() {
        return attributeLength;
    }

    @Override
    public String toString(ConstantPoolInfo[] poolInfos) {
        return "ConstantValue:"+poolInfos[constantValueIndex].toString(poolInfos);
    }
}
